﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PGMHelper;
using WHelper = PGMHelper.WilsonHelper;
using System.IO;
using Spectrum;
using System.Windows.Forms.DataVisualization.Charting;

namespace SpectrumTestForm
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 加速度激励时程
        /// </summary>
        private List<AccelParam> accelList { set; get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            //初始化加速度激励时程
            this.accelList = new List<AccelParam>();
            //设定CHART外观
            this.TestChart.SetApperance(StringAlignment.Near, true, true, true);
            this.SpectrumChart.SetApperance(StringAlignment.Near, true, true, false);
            //添加坐标轴信息
            this.TestChart.AddTitles("激励时程", "时间 (s)", "加\n速\n度");
            this.SpectrumChart.AddTitles("反应谱", "周期 (s)", "影\n响\n系\n数");
            //X向坐标取整数
            this.TestChart.SetAxisFormat(ChartAxisType.AxisX, AxisFormat.Normal, 0);
            this.SpectrumChart.SetAxisFormat(ChartAxisType.AxisX, AxisFormat.Normal, 0);
        }

        /// <summary>
        /// 测试按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestButton_Click(object sender, EventArgs e)
        {
            //初始化
            string filePath = string.Empty;
            //获得文件路径
            if (!WHelper.OpenFile("文本文档(*.txt)|*.txt|所有文件(*.*)|*.*", ref filePath))
                return;
            //构造时程
            this.accelList = new List<AccelParam>();
            //读取文件
            using (StreamReader sr = new StreamReader(filePath))
            {
                //初始化
                string line = string.Empty;
                //读取文件
                while((line = sr.ReadLine()) != null)
                {
                    //拆分语句
                    var strList = WHelper.SplitStr(line);
                    //拆分是否合法
                    if (strList.Count != 2) continue;
                    //添加值
                    this.accelList.Add(new AccelParam
                    {
                        Time = Convert.ToSingle(strList[0]),
                        Accel = Convert.ToSingle(strList[1]),
                    });
                }
            }
            //绘制加速度曲线
            this.DrawAccelList();
            //绘制反应谱曲线
            this.DrawSpectrum(true);
        }

        /// <summary>
        /// 绘制加速度激励时程
        /// </summary>
        private void DrawAccelList()
        {
            //清空序列
            this.TestChart.Series.Clear();
            //原激励时程
            this.TestChart.AddSeries("原激励", SeriesChartType.Line, Color.Red, 2);
            this.TestChart.Series["原激励"].Points.DataBindXY(
                this.GetLists(this.accelList, false), this.GetLists(this.accelList, true));
            //稀疏后激励时程
            var accelSet = new AccelSet(this.accelList);
            //获得激励时程函数
            var accelNew = accelSet.GetSpraseAccelHist(0.04F, 30);
            //绘图
            this.TestChart.AddSeries("稀疏激励", SeriesChartType.Line, Color.Black, 2);
            this.TestChart.Series["稀疏激励"].Points.DataBindXY(
                this.GetLists(accelNew, false), this.GetLists(accelNew, true));
        }

        /// <summary>
        /// 绘制加速度激励时程
        /// </summary>
        private void DrawSpectrum(bool isPeseudo = true)
        {
            //计算反应谱
            var spect = SpectrumMethod.GetResults(this.accelList, this.accelList, isPeseudo: isPeseudo);
            //清空序列
            this.SpectrumChart.Series.Clear();
            //反应谱曲线
            this.SpectrumChart.AddSeries("反应谱", SeriesChartType.Line, Color.Red, 2);
            this.SpectrumChart.Series["反应谱"].Points.DataBindXY(
                SpectrumMethod.GetListFromSpectList(spect, SpectrumType.Period),
                SpectrumMethod.GetListFromSpectList(spect, SpectrumType.Afa));
        }

        /// <summary>
        /// 获得加速度时程
        /// </summary>
        /// <param name="accels"></param>
        /// <returns></returns>
        private List<float> GetLists(List<AccelParam> accels, bool isAccel)
        {
            //属性名称
            string name = isAccel ? "Accel" : "Time";
            //获得列表
            return accels.GetPropertyList(name).ConvertAll(v => (float)v);
        } 
    }
}
